package com.xyh.reward_kotlin

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.xyh.reward_kotlin.databinding.ActivityMainBinding
import com.xyh.reward_kotlin.fragments.CalculatorFragment
import com.xyh.reward_kotlin.fragments.HistoryFragment
import com.xyh.reward_kotlin.fragments.HomeFragment
import com.xyh.reward_kotlin.fragments.ProfileFragment

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val fragments = arrayOf(
        HomeFragment(),
        CalculatorFragment(),
        HistoryFragment(),
        ProfileFragment()
    )
    private var currentFragmentIndex = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // ✅ 关闭系统默认 Insets 处理
        WindowCompat.setDecorFitsSystemWindows(window, false)

        // ✅ 状态栏透明
        window.statusBarColor = Color.TRANSPARENT

        // ✅ 状态栏图标变暗色（如果背景是浅色）
        WindowInsetsControllerCompat(window, window.decorView).apply {
            isAppearanceLightStatusBars = true
        }

        // ✅ 动态处理导航栏和底部间距
        binding.bottomNav.post {
            val bottomNavHeight = binding.bottomNav.height
            ViewCompat.setOnApplyWindowInsetsListener(binding.navHostFragment) { view, insets ->
                val systemBars: Insets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                view.setPadding(
                    0,
                    systemBars.top,
                    0,
                    systemBars.bottom + bottomNavHeight
                )
                insets
            }
            ViewCompat.requestApplyInsets(binding.navHostFragment)
        }

        // ✅ 底部导航栏点击切换 Fragment
        binding.bottomNav.setOnItemSelectedListener { item ->
            val view: View = binding.bottomNav.findViewById(item.itemId)
            view.animate()
                .scaleX(1.2f)
                .scaleY(1.2f)
                .setDuration(150)
                .withEndAction {
                    view.animate().scaleX(1f).scaleY(1f).setDuration(150).start()
                }
                .start()

            when (item.itemId) {
                R.id.nav_home -> switchFragment(0)
                R.id.nav_calculator -> switchFragment(1)
                R.id.nav_history -> switchFragment(2)
                R.id.nav_profile -> switchFragment(3)
                else -> return@setOnItemSelectedListener false
            }
            true
        }

        // 默认显示首页
        switchFragment(0)
    }

    fun switchFragment(index: Int) {
        if (currentFragmentIndex == index) return

        supportFragmentManager.beginTransaction().apply {
            // 添加自定义动画：左右滑动 + 淡入淡出
            setCustomAnimations(
                R.anim.slide_in_right,  // 新 Fragment 进入
                R.anim.slide_out_left,  // 当前 Fragment 离开
                R.anim.slide_in_left,   // 返回时新 Fragment 进入
                R.anim.slide_out_right  // 返回时当前 Fragment 离开
            )
            replace(R.id.nav_host_fragment, fragments[index])
            commit()
        }

        currentFragmentIndex = index
    }

    fun switchFragmentAndUpdateTab(index: Int) {
        switchFragment(index)
        binding.bottomNav.selectedItemId = when (index) {
            0 -> R.id.nav_home
            1 -> R.id.nav_calculator
            2 -> R.id.nav_history
            3 -> R.id.nav_profile
            else -> R.id.nav_home
        }
    }
}
